﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.LevelBusinessLogic;
using GovernmentOfficeProject.LevelDataAccess;
using GovernmentOfficeProject.SysTools;

namespace GovernmentOfficeProject.LevelViewUI.admin
{
    public partial class RoleManager : System.Web.UI.Page
    {
        BLL_RoleManager roleBLL = null;
        List<Entity_USER_Role> listRoleAll = null;
        //List<Entity_USER_Role> listRoleByCondition = null;
        //Entity_USER_Role roleInfo = null;
        private static bool blBtnSelectedRole = false;   //确定最后操作的按钮是条件搜索还是获取所有.
        private static bool blControlValueCheck = false; //确定控件值(用户输入)正确性验证后再进行操作.

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0 || SystemContext.IsAdmin == 0)
            {
                Response.Redirect("/LevelViewUI/admin/Login.htm?FromUrl=" + HttpUtility.UrlEncode(Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;
                SystemContext.IsAdmin = 1;
            }
            roleBLL = new BLL_RoleManager();
            listRoleAll = roleBLL.getRoleList();           
            if (!Page.IsPostBack)
            {
                gridDataBind();
                operationFeedbackByLabel("数据已加载完毕.");
                this.btnSave.Enabled = false;
                this.btnDelete.Enabled = false;
            }
        }

        //操作状态显示
        private void operationFeedbackByLabel(string strFeedback)
        {
            lblOperationState.Text = "操作状态:";
            lblOperationState.Text += strFeedback;
        }

        //为GridViewRoleList绑定所有用户组.
        private bool gridDataBind()
        {
            try
            {
                bool flag = false;
                if (listRoleAll != null)
                {
                    gridDataBind(listRoleAll);
                }
                else
                {
                    gridDataBind(roleBLL.getRoleList());
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }

        //根据传入list为GridViewRoleList绑定用户组.
        private bool gridDataBind(object objDataSource)
        {
            try
            {
                this.GridViewRoleList.DataSource = (List<Entity_USER_Role>)objDataSource;
                this.GridViewRoleList.DataBind();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
                throw ex;
            }
        }

        //使用自定义数据源,重新设置PageIndexChanging事件为实现分页正常.
        protected void GridViewRoleList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (blBtnSelectedRole)  //true:获取符合查询条件的用户组给GridViewRoleList绑定数据源.false:获取所有用户组给GridViewRoleList绑定数据源.
            {
                Entity_USER_Role role = new Entity_USER_Role();
                role.RoleName = this.txtSelectRoleName.Text.Trim();
                role.RecordStatus = Convert.ToDecimal(this.ddlSelectRoleState.SelectedValue);
                GridViewRoleList.PageIndex = e.NewPageIndex;
                gridDataBind(roleBLL.getRoleByCondition(role));
            }
            else
            {
                GridViewRoleList.PageIndex = e.NewPageIndex;
                gridDataBind(roleBLL.getRoleList());
            }
        }

        //GridView行中的选择用户组按钮:
        protected void btnSelectedRole_Click(object sender, EventArgs e)
        {
            this.lblMsRoleState.Text = "";
            this.lblMsRoleName.Text = "";
            this.btnDelete.Enabled = true;
            this.btnSave.Text = "修   改";
            this.btnSave.Enabled = true;
            string strRoleID = ((Button)sender).CommandArgument.ToString(); //获取选定主键值.
            decimal dclRoleID = Convert.ToDecimal(strRoleID.Trim());
            Entity_USER_Role role = roleBLL.getRoleByRoleID(dclRoleID);
            lblRoleIDValue.Text = role.RoleID.ToString();
            txtRoleName.Text = role.RoleName;
            txtRoleDesc.Text = role.RoleDesc;
            RadioButtonListRoleState.SelectedValue = role.RecordStatus.ToString();
            operationFeedbackByLabel("已选择用户组:" + role.RoleName + ",系统等待进行下一步操作.");
        }

        //用户组添加按钮:
        protected void btnAddRole_Click(object sender, EventArgs e)
        {
            operationFeedbackByLabel("正在创建新用户组...");
            this.lblRoleIDValue.Text = "";
            this.txtRoleName.Text = "";
            this.lblMsRoleName.Text = "*用户组名称为必填项.";
            this.txtRoleDesc.Text = "";
            this.RadioButtonListRoleState.SelectedValue = "1";
            this.lblMsRoleState.Text = "*新增用户组默认为启用状态";
            this.btnDelete.Enabled = false;
            this.btnSave.Text = "保   存";
            this.btnSave.Enabled = true;
        }

        //条件搜索按钮:
        protected void btnSelectByCondition_Click(object sender, EventArgs e)
        {
            blBtnSelectedRole = true;
            Entity_USER_Role roleCondition = new Entity_USER_Role();
            roleCondition.RoleName = this.txtSelectRoleName.Text.Trim();
            roleCondition.RecordStatus = Convert.ToDecimal(this.ddlSelectRoleState.SelectedValue.Trim());
            AppLog.Write("roleCondition:" + roleCondition.RecordStatus.ToString(), SysTools.AppLog.LogMessageType.Debug);
            List<Entity_USER_Role> roleConditionList = roleBLL.getRoleByCondition(roleCondition);
            if (roleConditionList.Count.Equals(0))
            {
                //blBtnSelectedRole = false;
                //gridDataBind();
                gridDataBind(null);
                operationFeedbackByLabel("没有符合条件的用户组,请检查搜索条件.");
            }
            else
            {
                if (gridDataBind(roleConditionList))
                {
                    operationFeedbackByLabel("已加载符合条件的" + roleConditionList.Count.ToString() + "个用户组.");
                }
            }
        }

        //所有用户组按钮:
        protected void btnSelectRoleAll_Click(object sender, EventArgs e)
        {
            blBtnSelectedRole = false;            
            gridDataBind();
            operationFeedbackByLabel("已加载所有用户组.");
        }

        //保存用户组按钮:
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (blControlValueCheck)
            {

                if (this.lblRoleIDValue.Text != "" && !Convert.ToDecimal(this.lblRoleIDValue.Text).Equals(0))   //lblRoleIDValue默认为0且选择用户组后值不为0)
                {
                    decimal roleID = Convert.ToDecimal(this.lblRoleIDValue.Text);
                    Entity_USER_Role role = new Entity_USER_Role();
                    role.RoleName = this.txtRoleName.Text.Trim();
                    role.RoleDesc = this.txtRoleDesc.Text.Trim();
                    role.RecordStatus = Convert.ToDecimal(this.RadioButtonListRoleState.SelectedValue);
                    if (roleBLL.updateRoleByRoleIDAndEntity(roleID, role))
                    {
                        GridViewRoleListBindSetting();
                        operationFeedbackByLabel("成功修改用户组:" + role.RoleName + ".");
                        this.lblRoleIDValue.Text = "";
                        this.txtRoleName.Text = "";
                        this.txtRoleDesc.Text = "";
                        this.RadioButtonListRoleState.SelectedValue = "1";
                        this.btnDelete.Enabled = false;
                        this.btnSave.Enabled = false;
                        this.lblRoleIDValue.Text = "";
                    }
                    else
                    {
                        operationFeedbackByLabel("修改用户组 " + role.RoleName + " 失败.");
                    }
                }
                else
                {
                    Entity_USER_Role role = new Entity_USER_Role();
                    role.RoleName = this.txtRoleName.Text.Trim();
                    role.RoleDesc = this.txtRoleDesc.Text.Trim();
                    role.RecordStatus = Convert.ToDecimal(this.RadioButtonListRoleState.SelectedValue);
                    if (roleBLL.addRole(role))
                    {
                        GridViewRoleListBindSetting();
                        operationFeedbackByLabel("成功新增用户组:" + role.RoleName + ".");
                        this.lblRoleIDValue.Text = "";
                        this.txtRoleName.Text = "";
                        this.txtRoleDesc.Text = "";
                        this.RadioButtonListRoleState.SelectedValue = "1";
                        this.lblRoleIDValue.Text = "";
                        this.btnDelete.Enabled = false;
                        this.btnSave.Enabled = false;
                    }
                    else
                    {
                        operationFeedbackByLabel("新增用户组 " + role.RoleName + " 失败.");
                    }
                }
            }
            else
            {
                operationFeedbackByLabel("请检查输入值的正确性.");
            }
        }

        //删除用户组按钮:(验证用户组下是否存在用户.)
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            decimal dclDeleteRoleID = Convert.ToDecimal(this.lblRoleIDValue.Text.Trim());
            Entity_USER_Role roleEntity = roleBLL.getRoleByRoleID(dclDeleteRoleID);
            int iUserCountByRole = 0;
            iUserCountByRole = roleBLL.getUserCountByRoleID(dclDeleteRoleID);
     //AppLog.Write("userCount:" + iUserCountByRole.ToString(), AppLog.LogMessageType.Debug);
            if (iUserCountByRole.Equals(0))
            {
                if (roleBLL.deleteRoleByRoleID(dclDeleteRoleID))    //如果删除成功.
                {
                    //删除被权限表、关系表引用的数据：
                    new BLL_Privilege().deletePrivilegeByMaster("PER_Role", dclDeleteRoleID);
                    //new BLL_UserRelation().de
                    GridViewRoleListBindSetting();
                    operationFeedbackByLabel("已删除用户组:" + roleEntity.RoleName + ".");
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.txtRoleDesc.Text = "";
                    this.txtRoleName.Text = "";
                    this.lblRoleIDValue.Text = "";
                }
                else
                {
                    operationFeedbackByLabel("删除用户组失败.");
                }
            }
            else
            {
                operationFeedbackByLabel("用户组:" + roleEntity.RoleName + "下存在多个用户,不能删除.");
            }
        }

        //根据当前情况重新绑定数据源:
        private void GridViewRoleListBindSetting()
        {
            string strRoleName = this.txtSelectRoleName.Text.Trim();
            decimal dclRoleState = Convert.ToDecimal(this.ddlSelectRoleState.SelectedValue);
            if (blBtnSelectedRole)  //最后一次是操作根据条件的搜索按钮.
            {
                Entity_USER_Role roleEntity = new Entity_USER_Role();
                if (strRoleName!="" && strRoleName!=null)
                {
                    roleEntity.RoleName = strRoleName;
                }
                roleEntity.RecordStatus = dclRoleState;
                gridDataBind(roleBLL.getRoleByCondition(roleEntity));
            }
            else
            {
                gridDataBind(roleBLL.getRoleList());
            }
        }

#region 验证控件输入值

        protected void txtRoleName_TextChanged(object sender, EventArgs e)
        {
            if (this.txtRoleName.Text.Length < 2 || this.txtRoleName.Text.Length > 16)
            {
                this.lblMsRoleName.Text = "*长度为2至16字符.";
                blControlValueCheck = false;
            }
            else
            {
                //验证数据库是否存在同名用户组.
                Entity_USER_Role roleEntity = new Entity_USER_Role();
                roleEntity.RoleName = this.txtRoleName.Text.Trim();
                if (roleBLL.getRoleByCondition(roleEntity).Count != 0)
                {
                    operationFeedbackByLabel("已存在同名用户组,请确认名称后继续操作.");
                    this.lblMsRoleName.Text = "*用户组名称重复,请确认.";
                    blControlValueCheck = false;
                }
                else
                {
                    this.lblMsRoleName.Text = "*";
                    blControlValueCheck = true;
                }
            }
        }

#endregion

    }
}
